package item

import (
	"context"
	"strings"

	"mall/service/recommended/api/internal/svc"
	"mall/service/recommended/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteItemLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteItemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteItemLogic {
	return &DeleteItemLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteItemLogic) DeleteItem(req *types.IdsReq) (resp *types.RowAffectedResp, err error) {
	if len(strings.TrimSpace(req.Ids)) == 0 {
		return &types.RowAffectedResp{}, nil
	}
	var rowAffected uint32
	for _, v := range strings.Split(req.Ids, ",") {
		res, err := l.svcCtx.GorseClient.DeleteItem(l.ctx, v)
		if err != nil {
			return nil, err
		}
		rowAffected += uint32(res.RowAffected)
	}

	return &types.RowAffectedResp{
		RowAffected: rowAffected,
	}, nil
}
